﻿using System;
using System.Collections.Generic;
using System.IO;

namespace IupNET
{
    public class FileDlg:IupBase
    {
        public FileDlg()
            : base(Iupc.FileDlg())
        {

        }

        public bool AllowNew
        {
            get
            {
                return GetAttribute("ALLOWNEW") == "YES";
            }
            set
            {
                SetAttribute("ALLOWNEW", value ? "YES" : "NO");

            }
        }

        public FileDialogType DialogType
        {
            get
            {
                return Convert.StringToFileDialogType(GetAttribute("DIALOGTYPE"));
            }
            set
            {
                SetAttribute("DIALOGTYPE",Convert.FileDialogTypeToString(value));

            }
        }


        public string Directory
        {
            get
            {
                return GetAttribute("DIRECTORY");
            }
            set
            {
                SetAttribute("DIRECTORY", value );

            }
        }

        public string ExtFilter
        {
            get
            {
                return GetAttribute("EXTFILTER");
            }
            set
            {
                SetAttribute("EXTFILTER", value);
            }
        }

        public string File
        {
            get
            {
                return GetAttribute("FILE");
            }
            set
            {
                SetAttribute("FILE", value);
            }
        }


        public bool FileExists
        {
            get
            {
                return GetAttribute("FILEEXISTS") == "YES";
            }
        }


        public string Filter
        {
            get
            {
                return GetAttribute("FILTER");
            }
            set
            {
                SetAttribute("FILTER", value);
            }
        }

        public string FilterInfo
        {
            get
            {
                return GetAttribute("FILTERINFO");
            }
            set
            {
                SetAttribute("FILTERINFO", value);
            }
        }


        public string FilterUsed
        {
            get
            {
                return GetAttribute("FILTERUSED");
            }
            set
            {
                SetAttribute("FILTERUSED", value);
            }
        }


        public bool MultipleFiles
        {
            get
            {
                return GetAttribute("MULTIPLEFILES") == "YES";
            }
            set
            {
                SetAttribute("MULTIPLEFILES", value ? "YES" : "NO");

            }
        }


        public bool NoChangeDir
        {
            get
            {
                return GetAttribute("NOCHANGEDIR") == "YES";
            }
            set
            {
                SetAttribute("NOCHANGEDIR", value ? "YES" : "NO");

            }
        }


        public bool NoOverwritePrompt
        {
            get
            {
                return GetAttribute("NOOVERWRITEPROMPT") == "YES";
            }
            set
            {
                SetAttribute("NOOVERWRITEPROMPT", value ? "YES" : "NO");

            }
        }

        public Dialog ParentDialog
        {
            get
            {
                return GetHandle(GetAttribute("PARENTDIALOG")) as Dialog;
            }

            set
            {
                SetAttribute("PARENTDIALOG", value.HandleName);
            }
        }



        public bool ShowHidden
        {
            get
            {
                return GetAttribute("SHOWHIDDEN") == "YES";
            }
            set
            {
                SetAttribute("SHOWHIDDEN", value ? "YES" : "NO");

            }
        }

        public bool ShowPreview
        {
            get
            {
                return GetAttribute("SHOWHIDDEN") == "YES";
            }
            set
            {
                SetAttribute("SHOWHIDDEN", value ? "YES" : "NO");

            }
        }

        public int Status
        {
            get
            {
                return Convert.StringToInt(GetAttribute("STATUS"));
            }
            set
            {
                SetAttribute("STATUS", value.ToString());
            }
        }

        public string Title
        {
            get
            {
                return GetAttribute("TITLE");
            }
            set
            {
                SetAttribute("TITLE", value);
            }
        }

        public string Value
        {
            get
            {
                return GetAttribute("VALUE");
            }
            set
            {
                SetAttribute("VALUE", value);
            }
        }

        public string[] SelectedFiles
        {
            get
            {
                List<String> res = new List<string>();
                string v = Value;
                if (v != null)
                {
                    if (v.IndexOf('|') >= 0)
                    { //multiple files
                        string[] split=v.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        for (int l = 1; l < split.Length; l++)
                        {
                            res.Add(split[0] + Path.DirectorySeparatorChar + split[l]);
                            
                        }
                    }
                    else
                        res.Add(v);
                }
                return res.ToArray();
            }
        }

        public EventCallback FileCB
        {
            get
            {
                return CBHandler.Fetch("FILE_CB");
            }
            set
            {
                CBHandler.Set("FILE_CB", value);
            }
        }

        public EventCallback HelpCB
        {
            get
            {
                return CBHandler.Fetch("HELP_CB");
            }
            set
            {
                CBHandler.Set("HELP_CB", value);
            }
        }

        public void Popup(int x, int y)
        {
            Iupc.Popup(cobject, x, y);
        }



    }
}
